package cn.demoa.alertdialog;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private Button simBtn;
    private Button popBtn;
    private Button radioBtn;
    private Button checkboxBtn;
    private Button customBtn;
    private Button customBtn2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        BindFindView();
        ShowDialog();
    }

    /*
     * 显示对话框
     * */
    private void ShowDialog() {
        ShowSimpleDialog();
        ShowPopDialog();
        showRadioDialog();
        showCheckboxDialog();
        showCustomDialog();
        showCustomDialog2();
    }

    /*
     * 自定义对话框
     * */
    private void showCustomDialog() {
        final EditText editText = new EditText(MainActivity.this);
        customBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AlertDialog.Builder(MainActivity.this)
                        .setTitle("请输入你的姓名")
                        .setView(editText)
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                String name = editText.getText().toString();
                                Toast.makeText(MainActivity.this, "姓名" + name, Toast.LENGTH_SHORT).show();
                            }
                        })
                        .create()
                        .show();
            }
        });
    }

    /*
     * 第二种自定义对话框
     * 使用自定义布局文件
     * */
    private void showCustomDialog2() {
        final View view = LayoutInflater.from(MainActivity.this)
                .inflate(R.layout.dialog_input, null, false);
        final EditText edtName = view.findViewById(R.id.edt_name);
        final EditText edtAge = view.findViewById(R.id.edt_age);

        customBtn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AlertDialog.Builder(MainActivity.this)
                        .setTitle("请输入你的信息")
                        .setView(view)
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                String name = edtName.getText().toString();
                                int age = Integer.parseInt(edtAge.getText().toString());
                                Toast.makeText(MainActivity.this,
                                        "姓名" + name + " 年龄" + age, Toast.LENGTH_SHORT).show();
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();
                            }
                        })
                        .create()
                        .show();
            }
        });

    }

    /*
     * 下拉多选框
     * */
    private void showCheckboxDialog() {
        final String[] hobbies = {"篮球", "足球", "羽毛球", "排球"};
        final StringBuffer buffer = new StringBuffer();
        checkboxBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                buffer.delete(0, buffer.length());//清除buffer的内容
                new AlertDialog.Builder(MainActivity.this)
                        .setIcon(R.mipmap.ic_launcher)
                        .setTitle("选择你的爱好")
                        /**
                         * 第一个参数指定我们要显示的一组下拉多选框的数据集合
                         * 第二个参数代表哪几个选项被选择，如果是null，则表示一个都不选择，如果希望指定哪一个多选选项框被选择，
                         * 需要传递一个boolean[]数组进去，其长度要和第一个参数的长度相同，例如 {true, false, false, true};
                         * 第三个参数给每一个多选项绑定一个监听器
                         */
                        .setMultiChoiceItems(hobbies, null, new DialogInterface.OnMultiChoiceClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                                if (isChecked) {
                                    buffer.append(hobbies[which] + " ");
                                }
                            }
                        })
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(MainActivity.this, "爱好为：" + buffer, Toast.LENGTH_SHORT).show();
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(MainActivity.this, "取消爱好选择", Toast.LENGTH_SHORT).show();
                            }
                        })
                        .create()
                        .show();
            }
        });
    }

    /*
     * 下拉单选框
     * */
    private void showRadioDialog() {
        final String[] sex = {"男", "女", "未知"};//设置下拉框的显示数据
        final StringBuffer buffer = new StringBuffer();
        radioBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AlertDialog.Builder(MainActivity.this)
                        .setIcon(R.mipmap.ic_launcher)
                        .setTitle("请选择性别")
                        /**
                         * 第一个参数指定我们要显示的一组下拉单选框的数据集合
                         * 第二个参数代表索引，指定默认哪一个单选框被勾选上，-1表示默认无默认勾选
                         * 第三个参数给每一个单选项绑定一个监听器
                         */
                        .setSingleChoiceItems(sex, -1, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                buffer.delete(0, buffer.length());
                                buffer.append(sex[which]);
                            }
                        })
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(MainActivity.this, "性别为" + buffer, Toast.LENGTH_SHORT).show();
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(MainActivity.this, "取消性别选择", Toast.LENGTH_SHORT).show();
                            }
                        })
                        .create()
                        .show();
            }
        });
    }

    /*
     * 下拉弹出框
     * */
    private void ShowPopDialog() {
        final String[] cites = {"刀山", "火海", "酆都", "鬼城", "奈何桥"};//设置下拉列表的显示数据
        popBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AlertDialog.Builder(MainActivity.this)
                        .setIcon(R.mipmap.ic_launcher)
                        .setTitle("你想去哪里旅游")
                        //设置下拉列表选择项
                        .setItems(cites, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(MainActivity.this,
                                        "你想去旅游的城市是" + cites[which],
                                        Toast.LENGTH_SHORT).show();
                            }
                        })
                        .create()
                        .show();
            }
        });
    }

    /*
     * 简单对话框
     * */
    private void ShowSimpleDialog() {
        simBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AlertDialog.Builder(MainActivity.this)
                        .setIcon(R.mipmap.ic_launcher)
                        .setTitle("提示")
                        .setMessage("你确定要删除吗")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(MainActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(MainActivity.this, "取消删除", Toast.LENGTH_SHORT).show();
                            }
                        })
                        .setNeutralButton("忽略", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(MainActivity.this, "啥也不干", Toast.LENGTH_SHORT).show();
                            }
                        })
                        .create()
                        .show();
            }
        });
    }

    /*
     * 绑定各个组件
     * */
    private void BindFindView() {
        simBtn = findViewById(R.id.sim_btn);
        popBtn = findViewById(R.id.pop_btn);
        radioBtn = findViewById(R.id.radio_btn);
        checkboxBtn = findViewById(R.id.checkbox_btn);
        customBtn = findViewById(R.id.custom_btn);
        customBtn2 = findViewById(R.id.custom_btn2);
    }
}
